﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Samples.WindowsAzure.ServiceManagement;

namespace WAPA.Framework
{
    public class SetupCloudTopologyAction : IAction
    {
        public  LocationList Locations;
        
        public SetupCloudTopologyAction()
        {
            Locations = new LocationList();
        }

        public void Execute()
        {
            //Procedural code for setting up the WAPA management environment.
            //Setup the queueus
            CloudServiceManager.SetupQueue(ManagementContext.RegistrationQueueName);
            CloudServiceManager.SetupQueue(ManagementContext.NotificationQueueName);
            CloudServiceManager.SetupQueue(ManagementContext.ReportQueueName);

            //Store the packages in a blob. For now we use prebuild packages for each VMSize option.
            //TODO:Finish the package updated so we can manipulate the service definitions.
            
            string extraSmallVMSizePackageFileName = ManagementContext.PackageFileName.Replace(".cspkg", "ExtraSmall.cspkg");
            string smallVMSizePackageFileName = ManagementContext.PackageFileName.Replace(".cspkg", "Small.cspkg");
            string mediumVMSizePackageFileName = ManagementContext.PackageFileName.Replace(".cspkg", "Medium.cspkg");
            string largeVMSizePackageFileName = ManagementContext.PackageFileName.Replace(".cspkg", "Large.cspkg");
            string extraLargeVMSizePackageFileName = ManagementContext.PackageFileName.Replace(".cspkg", "ExtraLarge.cspkg");
            
            string packgeFilePath = ManagementContext.ExecutionAssemblyLocation + "\\" +  ManagementContext.PackageFileFolder;

            CloudServiceManager.UploadPackageToBlobFromFile(new Uri(ManagementContext.BlobEndpointUri), ManagementContext.StorageAccountName, ManagementContext.AccessKey, System.IO.Path.Combine(packgeFilePath, extraSmallVMSizePackageFileName), ManagementContext.PackageContainerName, extraSmallVMSizePackageFileName, true);
            CloudServiceManager.UploadPackageToBlobFromFile(new Uri(ManagementContext.BlobEndpointUri), ManagementContext.StorageAccountName, ManagementContext.AccessKey, System.IO.Path.Combine(packgeFilePath, smallVMSizePackageFileName), ManagementContext.PackageContainerName, smallVMSizePackageFileName, true);
            CloudServiceManager.UploadPackageToBlobFromFile(new Uri(ManagementContext.BlobEndpointUri), ManagementContext.StorageAccountName, ManagementContext.AccessKey, System.IO.Path.Combine(packgeFilePath, mediumVMSizePackageFileName), ManagementContext.PackageContainerName, mediumVMSizePackageFileName, true);
            CloudServiceManager.UploadPackageToBlobFromFile(new Uri(ManagementContext.BlobEndpointUri), ManagementContext.StorageAccountName, ManagementContext.AccessKey, System.IO.Path.Combine(packgeFilePath, largeVMSizePackageFileName), ManagementContext.PackageContainerName, largeVMSizePackageFileName, true);
            CloudServiceManager.UploadPackageToBlobFromFile(new Uri(ManagementContext.BlobEndpointUri), ManagementContext.StorageAccountName, ManagementContext.AccessKey, System.IO.Path.Combine(packgeFilePath, extraLargeVMSizePackageFileName), ManagementContext.PackageContainerName, extraLargeVMSizePackageFileName, true);
            
            //Region options: Anywhere US,South Central US,North Central US,Anywhere Europe,North Europe,West Europe,Anywhere Asia,Southeast Asia,East Asia
            //Create the hosted service scenario
            List<string> serviceNames = new List<string>();
            foreach (Location location in Locations)
            {
                string serviceName = ScenarioManager.CreateHostedServiceByRegionName(location.Name);
                serviceNames.Add(serviceName);

                CloudServiceManager.SetupQueue(serviceName);
            }

            foreach (string serviceName in serviceNames)
            {
                //We need to add a certificate to the hosted service so we can manage it through the API. 
                //TODO:Find out if the management certificate is sufficient.
                ScenarioManager.AddCertificateToService(serviceName);
            }
        }
    }
}
